﻿namespace Chain.IDAL
{
    using Chain.DBUtility;
    using Chain.Model;
    using System;
    using System.Data;
    using System.Data.SqlClient;
    using System.Text;

    public class SysShopAuthority
    {
        public int Add(Chain.Model.SysShopAuthority model)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("insert into SysShopAuthority(");
            builder.Append("ShopAuthorityShopID,ShopAuthorityData)");
            builder.Append(" values (");
            builder.Append("@ShopAuthorityShopID,@ShopAuthorityData)");
            builder.Append(";select @@IDENTITY");
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@ShopAuthorityShopID", SqlDbType.Int, 4), new SqlParameter("@ShopAuthorityData", SqlDbType.VarChar, 0xfa0) };
            cmdParms[0].Value = model.ShopAuthorityShopID;
            cmdParms[1].Value = model.ShopAuthorityData;
            object single = DbHelperSQL.GetSingle(builder.ToString(), cmdParms);
            if (single == null)
            {
                return 0;
            }
            return Convert.ToInt32(single);
        }

        public Chain.Model.SysShopAuthority DataRowToModel(DataRow row)
        {
            Chain.Model.SysShopAuthority authority = new Chain.Model.SysShopAuthority();
            if (row != null)
            {
                if ((row["ShopAuthorityID"] != null) && (row["ShopAuthorityID"].ToString() != ""))
                {
                    authority.ShopAuthorityID = int.Parse(row["ShopAuthorityID"].ToString());
                }
                if ((row["ShopAuthorityShopID"] != null) && (row["ShopAuthorityShopID"].ToString() != ""))
                {
                    authority.ShopAuthorityShopID = new int?(int.Parse(row["ShopAuthorityShopID"].ToString()));
                }
                if (row["ShopAuthorityData"] != null)
                {
                    authority.ShopAuthorityData = row["ShopAuthorityData"].ToString();
                }
            }
            return authority;
        }

        public bool Delete(int ShopAuthorityID)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("delete from SysShopAuthority ");
            builder.Append(" where ShopAuthorityID=@ShopAuthorityID");
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@ShopAuthorityID", SqlDbType.Int, 4) };
            cmdParms[0].Value = ShopAuthorityID;
            return (DbHelperSQL.ExecuteSql(builder.ToString(), cmdParms) > 0);
        }

        public bool DeleteList(string ShopAuthorityIDlist)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("delete from SysShopAuthority ");
            builder.Append(" where ShopAuthorityID in (" + ShopAuthorityIDlist + ")  ");
            return (DbHelperSQL.ExecuteSql(builder.ToString()) > 0);
        }

        public bool Exists(int ShopAuthorityID)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select count(1) from SysShopAuthority");
            builder.Append(" where ShopAuthorityID=@ShopAuthorityID");
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@ShopAuthorityID", SqlDbType.Int, 4) };
            cmdParms[0].Value = ShopAuthorityID;
            return DbHelperSQL.Exists(builder.ToString(), cmdParms);
        }

        public DataSet GetList(string strWhere)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select ShopAuthorityID,ShopAuthorityShopID,ShopAuthorityData ");
            builder.Append(" FROM SysShopAuthority ");
            if (strWhere.Trim() != "")
            {
                builder.Append(" where " + strWhere);
            }
            return DbHelperSQL.Query(builder.ToString());
        }

        public DataSet GetList(int Top, string strWhere, string filedOrder)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select ");
            if (Top > 0)
            {
                builder.Append(" top " + Top.ToString());
            }
            builder.Append(" ShopAuthorityID,ShopAuthorityShopID,ShopAuthorityData ");
            builder.Append(" FROM SysShopAuthority ");
            if (strWhere.Trim() != "")
            {
                builder.Append(" where " + strWhere);
            }
            builder.Append(" order by " + filedOrder);
            return DbHelperSQL.Query(builder.ToString());
        }

        public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("SELECT * FROM ( ");
            builder.Append(" SELECT ROW_NUMBER() OVER (");
            if (!string.IsNullOrEmpty(orderby.Trim()))
            {
                builder.Append("order by T." + orderby);
            }
            else
            {
                builder.Append("order by T.ShopAuthorityID desc");
            }
            builder.Append(")AS Row, T.*  from SysShopAuthority T ");
            if (!string.IsNullOrEmpty(strWhere.Trim()))
            {
                builder.Append(" WHERE " + strWhere);
            }
            builder.Append(" ) TT");
            builder.AppendFormat(" WHERE TT.Row between {0} and {1}", startIndex, endIndex);
            return DbHelperSQL.Query(builder.ToString());
        }

        public int GetMaxId()
        {
            return DbHelperSQL.GetMaxID("ShopAuthorityID", "SysShopAuthority");
        }

        public Chain.Model.SysShopAuthority GetModel(int ShopAuthorityID)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select  top 1 ShopAuthorityID,ShopAuthorityShopID,ShopAuthorityData from SysShopAuthority ");
            builder.Append(" where ShopAuthorityID=@ShopAuthorityID");
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@ShopAuthorityID", SqlDbType.Int, 4) };
            cmdParms[0].Value = ShopAuthorityID;
            new Chain.Model.SysShopAuthority();
            DataSet set = DbHelperSQL.Query(builder.ToString(), cmdParms);
            if (set.Tables[0].Rows.Count > 0)
            {
                return this.DataRowToModel(set.Tables[0].Rows[0]);
            }
            return null;
        }

        public int GetRecordCount(string strWhere)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select count(1) FROM SysShopAuthority ");
            if (strWhere.Trim() != "")
            {
                builder.Append(" where " + strWhere);
            }
            object single = DbHelperSQL.GetSingle(builder.ToString());
            if (single == null)
            {
                return 0;
            }
            return Convert.ToInt32(single);
        }

        public DataSet GetShopAuthority(int ShopID)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append(" select * ");
            builder.Append(" from SysShopAuthority");
            builder.Append(" where ShopAuthorityShopID=" + ShopID);
            return DbHelperSQL.Query(builder.ToString());
        }

        public bool Update(Chain.Model.SysShopAuthority model)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("update SysShopAuthority set ");
            builder.Append("ShopAuthorityShopID=@ShopAuthorityShopID,");
            builder.Append("ShopAuthorityData=@ShopAuthorityData");
            builder.Append(" where ShopAuthorityID=@ShopAuthorityID");
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@ShopAuthorityShopID", SqlDbType.Int, 4), new SqlParameter("@ShopAuthorityData", SqlDbType.VarChar, 0xfa0), new SqlParameter("@ShopAuthorityID", SqlDbType.Int, 4) };
            cmdParms[0].Value = model.ShopAuthorityShopID;
            cmdParms[1].Value = model.ShopAuthorityData;
            cmdParms[2].Value = model.ShopAuthorityID;
            return (DbHelperSQL.ExecuteSql(builder.ToString(), cmdParms) > 0);
        }
    }
}

